/* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Pages
	DROP CONSTRAINT FK_me_Pages_me_Sites
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Pages
	DROP CONSTRAINT FK_Pages_Skins
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Pages
	DROP CONSTRAINT FK_Pages_Users
GO
ALTER TABLE dbo.Pages
	DROP CONSTRAINT FK_Pages_Users1
GO
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_Pages
	(
	Id int NOT NULL IDENTITY (1, 1),
	PageId uniqueidentifier NOT NULL,
	Name nvarchar(255) NOT NULL,
	ParentPageId int NULL,
	SkinId int NULL,
	SiteId uniqueidentifier NULL,
	Title nvarchar(255) NOT NULL,
	MenuTitle nvarchar(255) NULL,
	Alias nvarchar(255) NULL,
	Keywords nvarchar(MAX) NULL,
	Description nvarchar(MAX) NULL,
	CreatedDate datetime NOT NULL,
	ModifiedDate datetime NOT NULL,
	CreatedBy int NOT NULL,
	ModifiedBy int NOT NULL
	)  ON [PRIMARY]
	 TEXTIMAGE_ON [PRIMARY]
GO
SET IDENTITY_INSERT dbo.Tmp_Pages ON
GO
IF EXISTS(SELECT * FROM dbo.Pages)
	 EXEC('INSERT INTO dbo.Tmp_Pages (Id, PageId, Name, ParentPageId, SkinId, Title, MenuTitle, Alias, Keywords, Description, CreatedDate, ModifiedDate, CreatedBy, ModifiedBy)
		SELECT Id, PageId, Name, ParentPageId, SkinId, Title, MenuTitle, Alias, Keywords, Description, CreatedDate, ModifiedDate, CreatedBy, ModifiedBy FROM dbo.Pages WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_Pages OFF
GO
ALTER TABLE dbo.ItemList
	DROP CONSTRAINT FK_ItemList_Pages
GO
ALTER TABLE dbo.PageContents
	DROP CONSTRAINT FK_PageContents_Pages
GO
ALTER TABLE dbo.BlogTags
	DROP CONSTRAINT FK_BlogTags_Pages
GO
DROP TABLE dbo.Pages
GO
EXECUTE sp_rename N'dbo.Tmp_Pages', N'Pages', 'OBJECT' 
GO
ALTER TABLE dbo.Pages ADD CONSTRAINT
	PK_me_Pages PRIMARY KEY CLUSTERED 
	(
	Id
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
ALTER TABLE dbo.Pages ADD CONSTRAINT
	IX_Pages UNIQUE NONCLUSTERED 
	(
	PageId
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
ALTER TABLE dbo.Pages ADD CONSTRAINT
	FK_Pages_Users FOREIGN KEY
	(
	CreatedBy
	) REFERENCES dbo.Users
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.Pages ADD CONSTRAINT
	FK_Pages_Users1 FOREIGN KEY
	(
	ModifiedBy
	) REFERENCES dbo.Users
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.Pages ADD CONSTRAINT
	FK_Pages_Skins FOREIGN KEY
	(
	SkinId
	) REFERENCES dbo.Skins
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.Pages ADD CONSTRAINT
	FK_Pages_Sites FOREIGN KEY
	(
	SiteId
	) REFERENCES dbo.Sites
	(
	SiteId
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.BlogTags ADD CONSTRAINT
	FK_BlogTags_Pages FOREIGN KEY
	(
	PageId
	) REFERENCES dbo.Pages
	(
	PageId
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.PageContents ADD CONSTRAINT
	FK_PageContents_Pages FOREIGN KEY
	(
	PageId
	) REFERENCES dbo.Pages
	(
	PageId
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.ItemList ADD CONSTRAINT
	FK_ItemList_Pages FOREIGN KEY
	(
	PageId
	) REFERENCES dbo.Pages
	(
	PageId
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT

BEGIN TRANSACTION

UPDATE Pages
SET SiteId = '96916592-54b3-48de-8449-b9ffc8e59f6b'

ALTER TABLE Pages ALTER COLUMN SiteId uniqueidentifier NOT NULL

COMMIT
